package com.gexin.model.system;
import java.util.ArrayList;
import java.util.List;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.system.base.BaseAdminRole;
import com.jfinal.plugin.activerecord.Db;

/**
 * 业务名称:  t_admin_role(权限组表) 
 */
@SuppressWarnings("serial")
public class AdminRole extends BaseAdminRole<AdminRole> {
	public static final AdminRole dao = new AdminRole();
	/**
	 * 查询所有的权限组
	 * @return
	 */
	public List<AdminRole>  getAll(){
		return dao.find("select * from t_admin_role");
	}
	/**
	 * 保存权限
	 * @param roleid
	 * @param ids
	 */
	public void saveRole(Integer roleid,String ids){
		Db.update("DELETE from t_admin_role_res where role_id=?",roleid);
		String sp [] = ids.split(",");
		List<AdminRoleRes> list  =new ArrayList<AdminRoleRes>();
		for(String  item  : sp){
			if(StringUtil.isNotBlank(item)){
				AdminRoleRes res =new AdminRoleRes();
				res.setResId(Integer.parseInt(item));
				res.setRoleId(roleid);
				list.add(res);
			}
		
		}
		List<AdminUserRole>  role_list =  AdminUserRole.dao.find("select * from t_admin_user_role  where role_id=?",roleid);
		for(AdminUserRole  role :  role_list){
			RoleKit.clearUserRoleCache(role.getUserId());
		}
		Db.batchSave(list, list.size());
	}
}
